package com.ddw.wotcg.server.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Represents a utility class for hashing values
 * 
 * @author fungus1487
 * 
 */
public abstract class Hasher {
	
	/**
	 * Hashes a string value and returns the result
	 * 
	 * @param value
	 *            the value to hash
	 * @return a hashed value
	 */
	public static String hash(String value) {
		// Get digest instance and calculate the hash
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("SHA-256");
		} catch (final NoSuchAlgorithmException e) {
			throw new RuntimeException("Cannot get SHA-256 hash algorithm", e);
		}
		md.update(value.getBytes());
		
		// Get the byte data and convert to hex format
		final byte byteData[] = md.digest();
		final StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++) {
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));
		}
		
		return sb.toString();
	}
	
}
